package com.heima.juc.unlock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @author : leimingchao
 * @date Date : 2022年02月12日 20:26
 */
@Slf4j(topic = "c.Test6")
public class Test6 {

    public static void main(String[] args) {

        Student student = new Student();

        AtomicReferenceFieldUpdater updater = AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");

        System.out.println(updater.compareAndSet(student, null, "张三"));

        System.out.println(student);
    }
}

class Student {

    volatile String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
